package com.ml.doctor.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ml.doctor.R;


public class BasicItem extends LinearLayout {

    protected LinearLayout descContainer;
    protected TextView titleTextView;
    protected TextView descTextView;
    protected TextView subTitleTextView;
    protected ImageView iconImageView;
    protected TextView countTextView;
    protected ImageView moreImageView;

    public BasicItem(Context context) {
        this(context, null);
    }

    public BasicItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateLayout(context);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BasicItem);

        int iconResId = array.getResourceId(R.styleable.BasicItem_item_icon, 0);

        String title = array.getString(R.styleable.BasicItem_item_title);
        String titleHint = array.getString(R.styleable.BasicItem_item_titleHint);
        int defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, getResources()
                .getDisplayMetrics());
        int titleSize = array.getDimensionPixelSize(R.styleable.BasicItem_item_titleSize, defSize);
        int titleColor = array.getColor(R.styleable.BasicItem_item_titleColor, 0xff323232);

        defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources().getDisplayMetrics());
        int titleMinWidth = array.getDimensionPixelSize(R.styleable.BasicItem_item_titleMinWidth, defSize);

        String desc = array.getString(R.styleable.BasicItem_item_desc);
        String descHint = array.getString(R.styleable.BasicItem_item_descHint);
        defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
        int descSize = array.getDimensionPixelSize(R.styleable.BasicItem_item_descSize, defSize);
        int descColor = array.getColor(R.styleable.BasicItem_item_descColor, 0xff878787);

        String subTitle = array.getString(R.styleable.BasicItem_item_subTitle);
        String subTitleHint = array.getString(R.styleable.BasicItem_item_subTitleHint);
        defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
        int subTitleSize = array.getDimensionPixelSize(R.styleable.BasicItem_item_subTitleSize, defSize);
        int subTitleColor = array.getColor(R.styleable.BasicItem_item_subTitleColor, 0xff878787);

        String count = array.getString(R.styleable.BasicItem_item_count);
        defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics());
        int countSize = array.getDimensionPixelSize(R.styleable.BasicItem_item_countSize, defSize);
        int countColor = array.getColor(R.styleable.BasicItem_item_countColor, 0xff878787);

        int indicatorId = array.getResourceId(R.styleable.BasicItem_item_indicator, 0);
        array.recycle();

        findViews();

        setIcon(iconResId);

        setTitle(title);
        setTitleSize(titleSize);
        setTitleColor(titleColor);
        setTitleHint(titleHint);
        setTitleMinWidth(titleMinWidth);

        setDesc(desc);
        setDescSize(descSize);
        setDescColor(descColor);
        setDescHint(descHint);

        setSubTitle(subTitle);
        setSubTitleSize(subTitleSize);
        setSubTitleColor(subTitleColor);
        setSubTitleHint(subTitleHint);

        setCount(count);
        setCountColor(countColor);
        setCountSize(countSize);

        setIndicator(indicatorId);
    }

    protected void findViews() {
        iconImageView = (ImageView) findViewById(R.id.s_icon);
        titleTextView = (TextView) findViewById(R.id.s_title);
        descTextView = (TextView) findViewById(R.id.s_desc);
        subTitleTextView = (TextView) findViewById(R.id.s_subtitle);
        countTextView = (TextView) findViewById(R.id.s_count);
        moreImageView = (ImageView) findViewById(R.id.s_more);
        descContainer = (LinearLayout) findViewById(R.id.desc_container);
    }

    protected void inflateLayout(Context context) {
        inflate(context, R.layout.basic_item, this);
    }

    public ImageView getIcon() {
        return iconImageView;
    }

    public void setIconSize(int width, int height) {
        android.view.ViewGroup.LayoutParams lps = iconImageView.getLayoutParams();
        lps.width = width;
        lps.height = height;
        iconImageView.setLayoutParams(lps);
    }

    public void setIconEnable(boolean enabled) {
        iconImageView.setEnabled(enabled);
    }

    public boolean isIconEnabled() {
        return iconImageView.isEnabled();
    }

    public void setIconSelected(boolean selected) {
        iconImageView.setSelected(selected);
    }

    public boolean isIconSelected() {
        return iconImageView.isSelected();
    }

    public TextView getTitleView() {
        return titleTextView;
    }

    public TextView getDescTextView() {
        return descTextView;
    }

    public void addAttrView(View view) {
        int childCount = descContainer.getChildCount();
        if (childCount > 0) {
            descContainer.addView(view, childCount - 1);
        } else {
            descContainer.addView(view);
        }
    }

    public LinearLayout getDescContainer() {
        return descContainer;
    }

    public TextView getSubTitleView() {
        return subTitleTextView;
    }

    public TextView getCountView() {
        return countTextView;
    }

    public ImageView getIndicator() {
        return moreImageView;
    }

    public void setIndicatorSize(int width, int height) {
        android.view.ViewGroup.LayoutParams lps = moreImageView.getLayoutParams();
        lps.width = width;
        lps.height = height;
        moreImageView.setLayoutParams(lps);
    }

    public void setIndicatorEnable(boolean enabled) {
        moreImageView.setEnabled(enabled);
    }

    public boolean isIndicatorEnabled() {
        return moreImageView.isEnabled();
    }

    public void setIndicatorSelected(boolean selected) {
        moreImageView.setSelected(selected);
    }

    public boolean isIndicatorSelected() {
        return moreImageView.isSelected();
    }

    public void setIcon(int resId) {
        if (resId > 0) {
            iconImageView.setImageResource(resId);
        }
        if (iconImageView == null)
            return;
        iconImageView.setVisibility(resId > 0 ? VISIBLE : GONE);
    }

    public void setIcon(Drawable icon) {
        iconImageView.setImageDrawable(icon);
    }

    public void setTitle(CharSequence title) {
        if (titleTextView == null)
            return;
        titleTextView.setText(title);
    }

    public void setTitleHint(CharSequence hint) {
        titleTextView.setHint(hint);
    }

    public void setTitleSize(int size) {
        titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setTitleMinWidth(int size) {
        titleTextView.setMinWidth(size);
    }

    public void setTitleColor(int color) {
        titleTextView.setTextColor(color);
    }

    public void setSubTitle(CharSequence title) {
        subTitleTextView.setText(title);
    }

    public void setSubTitleHint(CharSequence hint) {
        subTitleTextView.setHint(hint);
    }

    public void setSubTitleSize(int size) {
        subTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setSubTitleColor(int color) {
        subTitleTextView.setTextColor(color);
    }

    public void setDesc(CharSequence desc) {
        descTextView.setText(desc);
    }

    public void setDescHint(CharSequence hint) {
        descTextView.setHint(hint);
    }


    public void setDescSize(int size) {
        descTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setDescColor(int color) {
        descTextView.setTextColor(color);
    }


    public void setCount(CharSequence count) {
        countTextView.setText(count);
        if (TextUtils.isEmpty(count)) {
            countTextView.setVisibility(GONE);
        } else {
            countTextView.setVisibility(VISIBLE);
        }
    }

    public void setCountSize(int size) {
        countTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    public void setCountColor(int value) {
        countTextView.setTextColor(value);
    }

    public void setIndicator(int drawableId) {
        if (drawableId > 0) {
            moreImageView.setImageResource(drawableId);
        }
        moreImageView.setVisibility(drawableId > 0 ? VISIBLE : GONE);
    }

    public void setIndicator(Drawable drawable) {
        moreImageView.setImageDrawable(drawable);
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        setIconEnable(clickable);
        setIndicatorEnable(clickable);
    }

    public void clearAll() {
        setTitle(null);
        setSubTitle(null);
        setCount(null);
        setDesc(null);
        int atts = descContainer.getChildCount();
        if (atts > 1) {
            descContainer.removeViews(0, atts - 1);
        }
    }

}
